package cn.t.service.user;

import cn.t.core.mode.YesNoEnum;
import cn.t.core.service.ServiceImpl;
import cn.t.core.enums.base.BaseStatusEnum;
import cn.t.model.user.RoleFirmDO;
import cn.t.repository.user.RoleFirmRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;


/**
 * @author l
 */
@Slf4j
@Service
@AllArgsConstructor
public class RoleFirmService extends ServiceImpl<RoleFirmDO, RoleFirmRepository> {

    public List<RoleFirmDO> findByRoleIds(Collection<Long> roleIdList) {

        if (roleIdList == null) {
            return null;
        }

        RoleFirmDO template = new RoleFirmDO();
        template.setRoleIds(roleIdList);
        template.setStatus(String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));
        template.setIsDeleted(YesNoEnum.FALSE);

        return findByModel(template, 0, 10000, null, null);
    }

    public List<RoleFirmDO> findByRoleId(Long roleId) {
        List<Long> roleIds = new ArrayList<>();
        roleIds.add(roleId);

        return findByRoleIds(roleIds);
    }

    public boolean removeByRoleId(Long roleId) {
        return repository.removeByRoleId(roleId);
    }

    public List<RoleFirmDO> list(Long firmId) {

        if (firmId == null) {
            return Collections.emptyList();
        }

        RoleFirmDO template = new RoleFirmDO();
        template.setFirmId(firmId);
        template.setStatus(String.valueOf(BaseStatusEnum.AVAILABLE.getCode()));

        return findByModel(template, 0, 10000, null, null);
    }

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;



}
